libname diabetes "X:\iDiabetes\Analysis\Data";
proc format;
value genderf 	1="Male"
				0="Female"
				;
value racecatf	1="White non-Hispanic"
    			2="Black non-Hispanic"
				3="Asian non-Hispanic"
				4="Hispanic"
    			5="Other"
				;
value racecat1f	1="White non-Hispanic"
    			2="Black non-Hispanic"
    			3="Other"
				;
value educationf	1="Less than high school"
					2="Some high school"
					3="High school graduate"
					4="Some college or specialized training"
					5="College or university graduate"
					;
value maritalcatf	1="Single"
					2="Married"
					3="Other"
					;
value incomecatf	1="Less than $50,000"
  					2="$50,000 to $100,000"
    				3="Greater than $100,000"
    				4="Missing"
					;
value householdf	1="1"
					2="2"
					3="3"
					4="4"
					5="5"
					6="6"
					7="7"
					8="8"
					9="More than 8"
					;
value healthf		1="Excellent"
					2="Very good"
					3="Good"
					4="Fair"
					5="Poor"
					;
value wirelesstechf	1="None"
					2="Little experience"
					3="Moderate experience"
					4="Very experienced"
					;
value stepgoalf	1="50%"
				2="40%"
				3="33%"
				;
 value wtgoalf	1="6%"
				2="7%"
				3="8%"
				;
value A1cgoalf	1="2.5% loss"
				2="2.0% loss"
				3="1.5% loss"
				;
value text_vs_emailf	1="Email"
						2="Text"
						3="Both"
						4="Neither"
						;
value monthf	1="JAN"
				2="FEB"
				3="MAR"
				4="APR"
				5="MAY"
				6="JUN"
				7="JUL"
				8="AUG"
				9="SEP"
				10="OCT"
				11="NOV"
				12="DEC"
				;
value loseweightf 	1="YES, MORE than 6 months"
					2="YES, LESS than 6 months"
					3="NO, 30 days"
					4="NO, 6 months"
					;
value endofstudyf	5="Strongly agree"
					4="Agree"
					3="Neutral"
					2="Disagree"
					1="Strongly disagree"
					;
run;


proc contents data=diabetes.participant_level_data varnum;
  run;

proc contents data=diabetes.hba1c_imputed_ varnum;
run;
proc contents data=diabetes.weights_imputed_ varnum;
run;
proc contents data=diabetes.LDL_imputed_ varnum;
run;
proc contents data=diabetes.steps_imputed_ varnum;
run;
proc contents data=diabetes.sleep_imputed_ varnum;
run;
 proc contents data=diabetes.steps_ varnum;/*Data collected in Way to Health*/
  run;
proc contents data=diabetes.sleep2;/*Data collected in Way to Health*/
 run;



/*Missing Data*/
%macro missing_check(data=,var=);
proc freq data=&data;
/*Use the two where statements below only for step/sleep data*/
/*where 1<=day_dummy<=182;*/
/*where day_dummy>182;*/
tables &var*arm_id/out=&var nopercent norow outpct;
run;
data &var.1;
set &var;
n_perc=cat(COUNT,"/",COUNT/(PCT_COL/100)," (",round(PCT_COL,.1),"%)"); 
if &var=0 then delete;
run;
proc transpose data=&var.1 out=&var.1_w;
var n_perc;
id arm_id;
run;
title "&var";
proc print data=&var.1_w noobs;
var A B C D;
run;
title;
%mend;

%missing_check(data=diabetes.participant_level_data,var=_6A1C_miss);
%missing_check(data=diabetes.participant_level_data,var=_12A1C_miss);
%missing_check(data=diabetes.participant_level_data,var=six_month_a1c_coal_miss);
%missing_check(data=diabetes.participant_level_data,var=twelve_month_a1c_coal_miss);
%missing_check(data=diabetes.participant_level_data,var=_6LDL_miss);
%missing_check(data=diabetes.participant_level_data,var=_12LDL_miss);
%missing_check(data=diabetes.participant_level_data,var=six_month_LDL_coal_miss);
%missing_check(data=diabetes.participant_level_data,var=twelve_month_LDL_coal_miss);
%missing_check(data=diabetes.participant_level_data,var=_6mowt_miss);
%missing_check(data=diabetes.participant_level_data,var=_12mowt_miss);
%missing_check(data=diabetes.participant_level_data,var=six_month_wt_coal_miss);
%missing_check(data=diabetes.participant_level_data,var=twelve_month_wt_coal_miss);

%missing_check(data=diabetes.steps_,var=steps_miss);/*Steps that were missing or 0*/
%missing_check(data=diabetes.steps_,var=steps1000_miss);/*Steps that were <1000*/
%missing_check(data=diabetes.steps_,var=steps_et_miss);/*Steps that were changed to missing due to event tracking*/
%missing_check(data=diabetes.sleep2,var=sleep_miss);/*Missing sleep durations*/
%missing_check(data=diabetes.sleep2,var=sleep_miss60);/*Sleep durations less than 60 minutes*/



/*Table 1*/
proc contents data=diabetes.baseline1;
run;

/*Macro for categorical variables*/
%macro categorical_summary(data=,var=,round=);
proc freq data=&data noprint;
tables &var*arm_id/out=&var._ missing outpct;
run;
data &var._1 (keep=&var arm_id n_pct);
set &var._;
n_pct=cat(count," (",round(PCT_COL,&round),"%)");
run;
proc transpose data=&var._1 out=&var._wide;
by &var;
var n_pct;
id arm_id;
run;
proc print data=&var._wide noobs;
var &var ;
run;
proc print data=&var._wide noobs;
var A B C D ;
run;
proc freq data=&data noprint;
tables &var/out=&var._overall missing ;
run;
data &var._overall1 (keep=&var n_pct);
set &var._overall;
n_pct=cat(count," (",round(percent,&round),"%)");
run;
proc print data=&var._overall1 noobs;
var &var ;
run;
proc print data=&var._overall1 noobs;
var n_pct;
run;
%mend;
%categorical_summary(data=diabetes.participant_level_data,var=sex,round=.1);
%categorical_summary(data=diabetes.participant_level_data,var=race_cat1,round=.1);
%categorical_summary(data=diabetes.participant_level_data,var=education,round=.1);
%categorical_summary(data=diabetes.participant_level_data,var=marital_cat,round=.1);
%categorical_summary(data=diabetes.participant_level_data,var=health,round=.1);
%categorical_summary(data=diabetes.participant_level_data,var=wear,round=.1);
%categorical_summary(data=diabetes.participant_level_data,var=scale,round=.1);
%categorical_summary(data=diabetes.participant_level_data,var=tech,round=.1);
%categorical_summary(data=diabetes.participant_level_data,var=facebook,round=.1);
%categorical_summary(data=diabetes.participant_level_data,var=twitter,round=.1);
%categorical_summary(data=diabetes.participant_level_data,var=instagram,round=.1);
%categorical_summary(data=diabetes.participant_level_data,var=snapchat,round=.1);
%categorical_summary(data=diabetes.participant_level_data,var=linkedin,round=.1);
%categorical_summary(data=diabetes.participant_level_data,var=pinterest,round=.1);
%categorical_summary(data=diabetes.participant_level_data,var=A1cgoal,round=.1);/*Output is in reverse order from table*/
%categorical_summary(data=diabetes.participant_level_data,var=wtgoal,round=.1);
%categorical_summary(data=diabetes.participant_level_data,var=stepgoal,round=.1);/*Output is in reverse order from table*/
%categorical_summary(data=diabetes.participant_level_data,var=smoke,round=.1);
%categorical_summary(data=diabetes.participant_level_data,var=tobacco,round=.1);
%categorical_summary(data=diabetes.participant_level_data,var=neuropathy,round=.1);
%categorical_summary(data=diabetes.participant_level_data,var=retinopathy,round=.1);
%categorical_summary(data=diabetes.participant_level_data,var=nephropathy,round=.1);
%categorical_summary(data=diabetes.participant_level_data,var=bp,round=.1);
%categorical_summary(data=diabetes.participant_level_data,var=chol,round=.1);
%categorical_summary(data=diabetes.participant_level_data,var=mi,round=.1);
%categorical_summary(data=diabetes.participant_level_data,var=chf,round=.1);
%categorical_summary(data=diabetes.participant_level_data,var=copd,round=.1);
%categorical_summary(data=diabetes.participant_level_data,var=cvd,round=.1);
%categorical_summary(data=diabetes.participant_level_data,var=ckd,round=.1);
%categorical_summary(data=diabetes.participant_level_data,var=Diabetes__insulin,round=.1);
%categorical_summary(data=diabetes.participant_level_data,var=Diabetes__oral,round=.1);
%categorical_summary(data=diabetes.participant_level_data,var=Statin,round=.1);

proc freq data=diabetes.participant_level_data;
tables (
sex race_cat1 education marital_cat health household incomecat wear scale tech 
facebook twitter instagram snapchat linkedin pinterest 
A1cgoal wtgoal stepgoal
smoke tobacco neuropathy retinopathy nephropathy bp chol mi chf copd cvd ckd 
Diabetes__insulin Diabetes__oral Statin)*arm_id/chisq norow nopercent;
run;

/*Macro for continuous variables*/

%macro cont_summary(data=,var=,round=,varnotequals=,missing=);
proc means data=&data nway noprint;
where &varnotequals &missing;
class arm_id;
var &var;
output out=&var. mean=mean std=std;
run;
data &var.1 (keep=arm_id mean_std);
set &var.;
mean_std=cat(round(mean,&round)," (",round(std,&round),")");
run;
proc transpose data=&var.1 out=&var.1_w;
var mean_std;
id arm_id;
run;
proc print data=&var.1_w noobs;
run;
proc means data=&data nway n nmiss printalltypes;
where &varnotequals &missing;
class arm_id;
var &var;
run;
proc means data=&data noprint;
where &varnotequals &missing;
var &var;
output out=&var._o mean=mean std=std;
run;
data &var._o1 (keep=mean_std);
set &var._o;
mean_std=cat(round(mean,&round)," (",round(std,&round),")");
run;
proc print data=&var._o1 noobs;
run;
proc means data=&data n nmiss;
where &varnotequals &missing;
var &var;
run;
%mend;
%cont_summary(data=diabetes.participant_level_data,var=age,round=0.1);
%cont_summary(data=diabetes.participant_level_data,var=Enrollment_A1C,round=0.1);
%cont_summary(data=diabetes.participant_level_data,var=baselinewt,round=0.1);
%cont_summary(data=diabetes.participant_level_data,var=bmi1,round=0.1);
%cont_summary(data=diabetes.participant_level_data,var=Enrollment_LDL1,round=0.1);
%cont_summary(data=diabetes.participant_level_data,var=baseline_steps,round=1);
%cont_summary(data=diabetes.participant_level_data,var=baseline_sleep,round=1);
%cont_summary(data=diabetes.participant_level_data,var=A1c_goal_decrease,round=0.01);
%cont_summary(data=diabetes.participant_level_data,var=weight_goal_decrease,round=0.1);
%cont_summary(data=diabetes.participant_level_data,var=step_goal_increase,round=1);
ods select classmeans KruskalWallisTest;
proc npar1way data =diabetes.participant_level_data;
class arm_id;
  var age Enrollment_A1C baselinewt bmi1 Enrollment_LDL1 baseline_steps A1c_goal_decrease weight_goal_decrease step_goal_increase;
run;
ods select all;

ods select classmeans KruskalWallisTest;
proc npar1way data =diabetes.participant_level_data;
class arm_id;
  var baseline_sleep;
run;
ods select all;
/*Unadjusted data: Imputed*/
%macro unadjusted_mean_imp(data=,var=,round=);
proc means data=&data nway noprint;
class participant_id arm_id;
var &var;
output out=mean mean=mean ;
run;
proc means data=mean nway;
class arm_id;
var mean;
output out=mean_std mean=mean std=std;
run;
data mean_std1;
set mean_std;
mean_std=cat(round(mean,&round)," (",round(std,&round),")");
run;
proc transpose data=mean_std1 out=mean_std1_w;
var mean_std;
id arm_id;
run;
proc print data=mean_std1_w noobs;
var A B C D;
run;
%mend;
%unadjusted_mean_imp(data=diabetes.hba1c_imputed_,var=Enrollment_A1C,round=.1);
%unadjusted_mean_imp(data=diabetes.hba1c_imputed_,var=six_month_a1c_coal,round=.1);
%unadjusted_mean_imp(data=diabetes.hba1c_imputed_,var=twelve_month_a1c_coal,round=.1);
%macro change_from_baseline(data=,var=,arm=);
proc sort data=&data;
by _imputation_;
run;
proc ttest data=&data h0=0;
where arm_id="&arm";
by _imputation_;
var &var;
ods output statistics=t_stats;
run;
ods trace on;
proc mianalyze data=t_stats;
modeleffects mean;
stderr stderr;
ods output parameterestimates=parest;
run;
ods trace off;
data parest1;
set parest;
est_cl=cat(round(Estimate,.01)," (",round(LCLMean,.01),", ",round(UCLMean,.01),")");
run;
title "&var. , &arm.";
proc print data=parest1 noobs;
var est_cl Probt;
run;
title "";
%mend;
%change_from_baseline(data=diabetes.hba1c_imputed_,var=six_month_a1c_coal_change,arm=A);
%change_from_baseline(data=diabetes.hba1c_imputed_,var=six_month_a1c_coal_change,arm=B);
%change_from_baseline(data=diabetes.hba1c_imputed_,var=six_month_a1c_coal_change,arm=C);
%change_from_baseline(data=diabetes.hba1c_imputed_,var=six_month_a1c_coal_change,arm=D);

%change_from_baseline(data=diabetes.hba1c_imputed_,var=twelve_month_a1c_coal_change,arm=A);
%change_from_baseline(data=diabetes.hba1c_imputed_,var=twelve_month_a1c_coal_change,arm=B);
%change_from_baseline(data=diabetes.hba1c_imputed_,var=twelve_month_a1c_coal_change,arm=C);
%change_from_baseline(data=diabetes.hba1c_imputed_,var=twelve_month_a1c_coal_change,arm=D);

proc sql;
create table imputed_a1c_cat as 
select distinct arm_id,participant_id,
case when .<mean(six_month_a1c_coal)<=7 then 1 
	when mean(six_month_a1c_coal)>7 then 0
else . end as six_month_a1c_less7,
case when .<mean(twelve_month_a1c_coal)<=7 then 1 
	when mean(twelve_month_a1c_coal)>7 then 0
else . end as twelve_month_a1c_less7,
case when .<mean(six_month_a1c_coal)<=8 then 1 
	when mean(six_month_a1c_coal)>8 then 0
else . end as six_month_a1c_less8,
case when .<mean(twelve_month_a1c_coal)<=8 then 1 
	when mean(twelve_month_a1c_coal)>8 then 0
else . end as twelve_month_a1c_less8 
from diabetes.hba1c_imputed_
group by arm_id,participant_id
;
quit;
proc freq data=imputed_a1c_cat;
tables (six_month_a1c_less7 six_month_a1c_less8 twelve_month_a1c_less7 twelve_month_a1c_less8)*arm_id/norow nopercent ;
run;

%macro combchi(data=,var=,arm=);
/*https://www.sas.upenn.edu/~allison/combchi.sas*/
title " &var. : &arm.";
proc sort data=&data;
by _imputation_;
run;
proc freq data=&data noprint;
by _imputation_;
where arm_id in ("&arm.","D");
tables (&var )*arm_id/norow nopercent chisq;
output out=chisq n PCHI;
run;
Proc Sql noprint;
Select _PCHI_ into :chi separated by ' '
from chisq/*)*/
;
Quit;
proc iml;
  df=1;
  g2={&chi};
  m=ncol(g2);
  g=sqrt(g2);
  mg2=sum(g2)/m;
  r=(1+1/m)*(ssq(g)-(sum(g)**2)/m)/(m-1);
  f=(mg2/df - r*(m-1)/(m+1))/(1+r);
  ddf=(m-1)*(1+1/r)**2/df**(3/m);
  p=round(1-probf(f,df,ddf),.0001);
  print f df ddf;
  print p;
quit;
title;
%mend;
%combchi(data=diabetes.hba1c_imputed_,var=six_month_a1c_less7,arm=A);
%combchi(data=diabetes.hba1c_imputed_,var=six_month_a1c_less7,arm=B);
%combchi(data=diabetes.hba1c_imputed_,var=six_month_a1c_less7,arm=C);

%combchi(data=diabetes.hba1c_imputed_,var=six_month_a1c_less8,arm=A);
%combchi(data=diabetes.hba1c_imputed_,var=six_month_a1c_less8,arm=B);
%combchi(data=diabetes.hba1c_imputed_,var=six_month_a1c_less8,arm=C);

%combchi(data=diabetes.hba1c_imputed_,var=twelve_month_a1c_less7,arm=A);
%combchi(data=diabetes.hba1c_imputed_,var=twelve_month_a1c_less7,arm=B);
%combchi(data=diabetes.hba1c_imputed_,var=twelve_month_a1c_less7,arm=C);

%combchi(data=diabetes.hba1c_imputed_,var=twelve_month_a1c_less8,arm=A);
%combchi(data=diabetes.hba1c_imputed_,var=twelve_month_a1c_less8,arm=B);
%combchi(data=diabetes.hba1c_imputed_,var=twelve_month_a1c_less8,arm=C);

%unadjusted_mean_imp(data=diabetes.weights_imputed_,var=baselinewt,round=.1);
%unadjusted_mean_imp(data=diabetes.weights_imputed_,var=six_month_wt_coal,round=.1);
%unadjusted_mean_imp(data=diabetes.weights_imputed_,var=twelve_month_wt_coal,round=.1);

%change_from_baseline(data=diabetes.weights_imputed_,var=six_month_wt_coal_change,arm=A);
%change_from_baseline(data=diabetes.weights_imputed_,var=six_month_wt_coal_change,arm=B);
%change_from_baseline(data=diabetes.weights_imputed_,var=six_month_wt_coal_change,arm=C);
%change_from_baseline(data=diabetes.weights_imputed_,var=six_month_wt_coal_change,arm=D);

%change_from_baseline(data=diabetes.weights_imputed_,var=twelve_month_wt_coal_change,arm=A);
%change_from_baseline(data=diabetes.weights_imputed_,var=twelve_month_wt_coal_change,arm=B);
%change_from_baseline(data=diabetes.weights_imputed_,var=twelve_month_wt_coal_change,arm=C);
%change_from_baseline(data=diabetes.weights_imputed_,var=twelve_month_wt_coal_change,arm=D);

proc sql;
create table imputed_weight_cat as
select distinct arm_id,participant_id,
case when .<mean(six_month_wt_coal)<=.93*mean(baselinewt) then 1
	when mean(six_month_wt_coal)>.93*mean(baselinewt) then 0
else . end as six_month_wt_coal_7ploss,
case when .<mean(six_month_wt_coal)<=.95*mean(baselinewt) then 1
	when mean(six_month_wt_coal)>.95*mean(baselinewt) then 0
else . end as six_month_wt_coal_5ploss,
case when .<mean(twelve_month_wt_coal)<=.93*mean(baselinewt) then 1
	when mean(twelve_month_wt_coal)>.93*mean(baselinewt) then 0
else . end as twelve_month_wt_coal_7ploss,
case when .<mean(twelve_month_wt_coal)<=.95*mean(baselinewt) then 1
	when mean(twelve_month_wt_coal)>.95*mean(baselinewt) then 0
else . end as twelve_month_wt_coal_5ploss 
from diabetes.weights_imputed_
group by arm_id,participant_id
;
quit;

proc freq data=imputed_weight_cat;
tables (six_month_wt_coal_5ploss six_month_wt_coal_7ploss twelve_month_wt_coal_5ploss twelve_month_wt_coal_7ploss)*arm_id/norow nopercent;
run;

%combchi(data=diabetes.weights_imputed_,var=six_month_wt_coal_5ploss,arm=A);
%combchi(data=diabetes.weights_imputed_,var=six_month_wt_coal_5ploss,arm=B);
%combchi(data=diabetes.weights_imputed_,var=six_month_wt_coal_5ploss,arm=C);

%combchi(data=diabetes.weights_imputed_,var=six_month_wt_coal_7ploss,arm=A);
%combchi(data=diabetes.weights_imputed_,var=six_month_wt_coal_7ploss,arm=B);
%combchi(data=diabetes.weights_imputed_,var=six_month_wt_coal_7ploss,arm=C);

%combchi(data=diabetes.weights_imputed_,var=twelve_month_wt_coal_5ploss,arm=A);
%combchi(data=diabetes.weights_imputed_,var=twelve_month_wt_coal_5ploss,arm=B);
%combchi(data=diabetes.weights_imputed_,var=twelve_month_wt_coal_5ploss,arm=C);

%combchi(data=diabetes.weights_imputed_,var=twelve_month_wt_coal_7ploss,arm=A);
%combchi(data=diabetes.weights_imputed_,var=twelve_month_wt_coal_7ploss,arm=B);
%combchi(data=diabetes.weights_imputed_,var=twelve_month_wt_coal_7ploss,arm=C);

%unadjusted_mean_imp(data=diabetes.baseline1,var=Enrollment_LDL1,round=.1);
%unadjusted_mean_imp(data=diabetes.LDL_imputed_,var=six_month_LDL_coal,round=.1);
%unadjusted_mean_imp(data=diabetes.LDL_imputed_,var=twelve_month_LDL_coal,round=.1);

%macro unadjusted_steps(data=,var=,week_start=,week_end=);
proc means data=&data nway noprint;
where &week_start<=week<=&week_end;
class arm_id participant_id;
var &var;
output out=mean mean=mean ;
run;
proc means data=mean nway noprint ;
class arm_id;
var mean;
output out=mean1 mean=mean std=std;
run;
data mean1_;
set mean1;
mean_std=cat(round(mean,1)/*," (",round(std,1),")"*/);
run;
proc transpose data=mean1_ out=mean1_wide;
var mean_std;
id arm_id;
run;
proc print data=mean1_wide noobs;
var A B C D;
run;
%mend;
%unadjusted_mean(data=diabetes.baseline1,var=baseline_steps,round=1);
%unadjusted_steps(data=diabetes.steps_imputed_,var=steps1000_et,week_start=1,week_end=26);
%unadjusted_steps(data=diabetes.steps_imputed_,var=steps1000_et,week_start=27,week_end=52);
%unadjusted_steps(data=diabetes.steps_imputed_,var=steps1000_et,week_start=1,week_end=52);

%unadjusted_mean(data=diabetes.baseline1,var=baseline_steps,round=1);
%unadjusted_steps(data=diabetes.steps_imputed_,var=steps1000_et,week_start=1,week_end=4);
%unadjusted_steps(data=diabetes.steps_imputed_,var=steps1000_et,week_start=5,week_end=8);
%unadjusted_steps(data=diabetes.steps_imputed_,var=steps1000_et,week_start=9,week_end=12);
%unadjusted_steps(data=diabetes.steps_imputed_,var=steps1000_et,week_start=13,week_end=16);
%unadjusted_steps(data=diabetes.steps_imputed_,var=steps1000_et,week_start=17,week_end=20);
%unadjusted_steps(data=diabetes.steps_imputed_,var=steps1000_et,week_start=21,week_end=24);
%unadjusted_steps(data=diabetes.steps_imputed_,var=steps1000_et,week_start=25,week_end=28);
%unadjusted_steps(data=diabetes.steps_imputed_,var=steps1000_et,week_start=29,week_end=32);
%unadjusted_steps(data=diabetes.steps_imputed_,var=steps1000_et,week_start=33,week_end=36);
%unadjusted_steps(data=diabetes.steps_imputed_,var=steps1000_et,week_start=37,week_end=40);
%unadjusted_steps(data=diabetes.steps_imputed_,var=steps1000_et,week_start=41,week_end=44);
%unadjusted_steps(data=diabetes.steps_imputed_,var=steps1000_et,week_start=45,week_end=48);
%unadjusted_steps(data=diabetes.steps_imputed_,var=steps1000_et,week_start=49,week_end=52);

%unadjusted_mean(data=diabetes.baseline1,var=baseline_steps_less1000,round=1);
%unadjusted_steps(data=diabetes.steps_imputed_,var=steps_et,week_start=1,week_end=26);
%unadjusted_steps(data=diabetes.steps_imputed_,var=steps_et,week_start=27,week_end=52);
%unadjusted_steps(data=diabetes.steps_imputed_,var=steps_et,week_start=1,week_end=52);

%unadjusted_mean(data=diabetes.baseline1,var=baseline_steps_less1000,round=1);
%unadjusted_steps(data=diabetes.steps_imputed_,var=steps_et,week_start=1,week_end=4);
%unadjusted_steps(data=diabetes.steps_imputed_,var=steps_et,week_start=5,week_end=8);
%unadjusted_steps(data=diabetes.steps_imputed_,var=steps_et,week_start=9,week_end=12);
%unadjusted_steps(data=diabetes.steps_imputed_,var=steps_et,week_start=13,week_end=16);
%unadjusted_steps(data=diabetes.steps_imputed_,var=steps_et,week_start=17,week_end=20);
%unadjusted_steps(data=diabetes.steps_imputed_,var=steps_et,week_start=21,week_end=24);
%unadjusted_steps(data=diabetes.steps_imputed_,var=steps_et,week_start=25,week_end=28);
%unadjusted_steps(data=diabetes.steps_imputed_,var=steps_et,week_start=29,week_end=32);
%unadjusted_steps(data=diabetes.steps_imputed_,var=steps_et,week_start=33,week_end=36);
%unadjusted_steps(data=diabetes.steps_imputed_,var=steps_et,week_start=37,week_end=40);
%unadjusted_steps(data=diabetes.steps_imputed_,var=steps_et,week_start=41,week_end=44);
%unadjusted_steps(data=diabetes.steps_imputed_,var=steps_et,week_start=45,week_end=48);
%unadjusted_steps(data=diabetes.steps_imputed_,var=steps_et,week_start=49,week_end=52);

%unadjusted_mean(data=diabetes.baseline_sleep,var=baseline_sleep,round=1);
%unadjusted_steps(data=diabetes.sleep_imputed_,var=total_sleep_duration_m,week_start=1,week_end=26);
%unadjusted_steps(data=diabetes.sleep_imputed_,var=total_sleep_duration_m,week_start=27,week_end=52);
%unadjusted_steps(data=diabetes.sleep_imputed_,var=total_sleep_duration_m,week_start=1,week_end=52);

%unadjusted_mean(data=diabetes.baseline_sleep,var=baseline_sleep,round=1);
%unadjusted_steps(data=diabetes.sleep_imputed_,var=total_sleep_duration_m,week_start=1,week_end=4);
%unadjusted_steps(data=diabetes.sleep_imputed_,var=total_sleep_duration_m,week_start=5,week_end=8);
%unadjusted_steps(data=diabetes.sleep_imputed_,var=total_sleep_duration_m,week_start=9,week_end=12);
%unadjusted_steps(data=diabetes.sleep_imputed_,var=total_sleep_duration_m,week_start=13,week_end=16);
%unadjusted_steps(data=diabetes.sleep_imputed_,var=total_sleep_duration_m,week_start=17,week_end=20);
%unadjusted_steps(data=diabetes.sleep_imputed_,var=total_sleep_duration_m,week_start=21,week_end=24);
%unadjusted_steps(data=diabetes.sleep_imputed_,var=total_sleep_duration_m,week_start=25,week_end=28);
%unadjusted_steps(data=diabetes.sleep_imputed_,var=total_sleep_duration_m,week_start=29,week_end=32);
%unadjusted_steps(data=diabetes.sleep_imputed_,var=total_sleep_duration_m,week_start=33,week_end=36);
%unadjusted_steps(data=diabetes.sleep_imputed_,var=total_sleep_duration_m,week_start=37,week_end=40);
%unadjusted_steps(data=diabetes.sleep_imputed_,var=total_sleep_duration_m,week_start=41,week_end=44);
%unadjusted_steps(data=diabetes.sleep_imputed_,var=total_sleep_duration_m,week_start=45,week_end=48);
%unadjusted_steps(data=diabetes.sleep_imputed_,var=total_sleep_duration_m,week_start=49,week_end=52);
/*Unadjusted data: Collected*/
%macro unadjusted_mean(data=,var=,round=);
proc means data=&data nway;
class arm_id;
var &var;
output out=mean_std mean=mean std=std;
run;
data mean_std1;
set mean_std;
mean_std=cat(round(mean,&round)," (",round(std,&round),")");
run;
proc transpose data=mean_std1 out=mean_std1_w;
var mean_std;
id arm_id;
run;
proc print data=mean_std1_w noobs;
var A B C D;
run;
%mend;
%unadjusted_mean(data=diabetes.participant_level_data,var=Enrollment_A1C,round=.1);
%unadjusted_mean(data=diabetes.participant_level_data,var=six_month_a1c_coal,round=.1);
%unadjusted_mean(data=diabetes.participant_level_data,var=twelve_month_a1c_coal,round=.1);
proc freq data=diabetes.participant_level_data;
tables (six_month_a1c_coal_goal six_month_a1c_pl1 six_month_a1c_less7 six_month_a1c_less8 
twelve_month_a1c_coal_goal twelve_month_a1c_pl1 twelve_month_a1c_less7 twelve_month_a1c_less8)*arm_id/norow nopercent;
run;
proc freq data=diabetes.participant_level_data;
where arm_id in ("A","D");
tables (six_month_a1c_less7 six_month_a1c_less8 twelve_month_a1c_less7 twelve_month_a1c_less8)*arm_id/norow nopercent chisq;
run;
proc freq data=diabetes.participant_level_data;
where arm_id in ("B","D");
tables (six_month_a1c_less7 six_month_a1c_less8 twelve_month_a1c_less7 twelve_month_a1c_less8)*arm_id/norow nopercent  chisq;
run;
proc freq data=diabetes.participant_level_data;
where arm_id in ("C","D");
tables (six_month_a1c_less7 six_month_a1c_less8 twelve_month_a1c_less7 twelve_month_a1c_less8)*arm_id/norow nopercent  chisq;
run;
%unadjusted_mean(data=diabetes.participant_level_data,var=baselinewt,round=.1);
%unadjusted_mean(data=diabetes.participant_level_data,var=six_month_wt_coal,round=.1);
%unadjusted_mean(data=diabetes.participant_level_data,var=twelve_month_wt_coal,round=.1);
proc freq data=diabetes.participant_level_data;
tables (six_month_wt_coal_goal twelve_month_wt_coal_goal)*arm_id/norow nopercent;
run;
proc freq data=diabetes.participant_level_data;
where arm_id in ("A","D");
tables (six_month_wt_coal_5ploss six_month_wt_coal_7ploss twelve_month_wt_coal_5ploss twelve_month_wt_coal_7ploss)*arm_id/norow nopercent chisq;
run;
proc freq data=diabetes.participant_level_data;
where arm_id in ("B","D");
tables (six_month_wt_coal_5ploss six_month_wt_coal_7ploss twelve_month_wt_coal_5ploss twelve_month_wt_coal_7ploss)*arm_id/norow nopercent chisq;
run;
proc freq data=diabetes.participant_level_data;
where arm_id in ("C","D");
tables (six_month_wt_coal_5ploss six_month_wt_coal_7ploss twelve_month_wt_coal_5ploss twelve_month_wt_coal_7ploss)*arm_id/norow nopercent chisq;
run;
%unadjusted_mean(data=diabetes.participant_level_data,var=Enrollment_LDL1,round=.1);
%unadjusted_mean(data=diabetes.participant_level_data,var=six_month_LDL_coal,round=.1);
%unadjusted_mean(data=diabetes.participant_level_data,var=twelve_month_LDL_coal,round=.1);
%unadjusted_mean(data=diabetes.participant_level_data,var=baseline_steps,round=1);
%unadjusted_mean(data=diabetes.participant_level_data,var=baseline_steps_less1000,round=1);
%unadjusted_mean(data=diabetes.participant_level_data,var=baseline_sleep,round=1);
%macro unadjusted_steps(data=,var=,week_start=,week_end=);
proc means data=&data nway noprint;
where &week_start<=week<=&week_end;
class arm_id participant_id;
var &var;
output out=mean mean=mean ;
run;
proc means data=mean nway noprint ;
class arm_id;
var mean;
output out=mean1 mean=mean std=std;
run;
data mean1_;
set mean1;
mean_std=cat(round(mean,1)," (",round(std,1),")");
run;
proc transpose data=mean1_ out=mean1_wide;
var mean_std;
id arm_id;
run;
proc print data=mean1_wide noobs;
var A B C D;
run;
%mend;
%unadjusted_mean(data=diabetes.participant_level_data,var=baseline_steps,round=1);
%unadjusted_steps(data=diabetes.steps_,var=steps1000_et,week_start=1,week_end=26);
%unadjusted_steps(data=diabetes.steps_,var=steps1000_et,week_start=27,week_end=52);
%unadjusted_steps(data=diabetes.steps_,var=steps1000_et,week_start=1,week_end=52);

%unadjusted_mean(data=diabetes.participant_level_data,var=baseline_steps,round=1);
%unadjusted_steps(data=diabetes.steps_,var=steps1000_et,week_start=1,week_end=4);
%unadjusted_steps(data=diabetes.steps_,var=steps1000_et,week_start=5,week_end=8);
%unadjusted_steps(data=diabetes.steps_,var=steps1000_et,week_start=9,week_end=12);
%unadjusted_steps(data=diabetes.steps_,var=steps1000_et,week_start=13,week_end=16);
%unadjusted_steps(data=diabetes.steps_,var=steps1000_et,week_start=17,week_end=20);
%unadjusted_steps(data=diabetes.steps_,var=steps1000_et,week_start=21,week_end=24);
%unadjusted_steps(data=diabetes.steps_,var=steps1000_et,week_start=25,week_end=28);
%unadjusted_steps(data=diabetes.steps_,var=steps1000_et,week_start=29,week_end=32);
%unadjusted_steps(data=diabetes.steps_,var=steps1000_et,week_start=33,week_end=36);
%unadjusted_steps(data=diabetes.steps_,var=steps1000_et,week_start=37,week_end=40);
%unadjusted_steps(data=diabetes.steps_,var=steps1000_et,week_start=41,week_end=44);
%unadjusted_steps(data=diabetes.steps_,var=steps1000_et,week_start=45,week_end=48);
%unadjusted_steps(data=diabetes.steps_,var=steps1000_et,week_start=49,week_end=52);

%unadjusted_mean(data=diabetes.participant_level_data,var=baseline_steps_less1000,round=1);
%unadjusted_steps(data=diabetes.steps_,var=steps_et,week_start=1,week_end=26);
%unadjusted_steps(data=diabetes.steps_,var=steps_et,week_start=27,week_end=52);
%unadjusted_steps(data=diabetes.steps_,var=steps_et,week_start=1,week_end=52);

%unadjusted_mean(data=diabetes.participant_level_data,var=baseline_steps_less1000,round=1);
%unadjusted_steps(data=diabetes.steps_,var=steps_et,week_start=1,week_end=4);
%unadjusted_steps(data=diabetes.steps_,var=steps_et,week_start=5,week_end=8);
%unadjusted_steps(data=diabetes.steps_,var=steps_et,week_start=9,week_end=12);
%unadjusted_steps(data=diabetes.steps_,var=steps_et,week_start=13,week_end=16);
%unadjusted_steps(data=diabetes.steps_,var=steps_et,week_start=17,week_end=20);
%unadjusted_steps(data=diabetes.steps_,var=steps_et,week_start=21,week_end=24);
%unadjusted_steps(data=diabetes.steps_,var=steps_et,week_start=25,week_end=28);
%unadjusted_steps(data=diabetes.steps_,var=steps_et,week_start=29,week_end=32);
%unadjusted_steps(data=diabetes.steps_,var=steps_et,week_start=33,week_end=36);
%unadjusted_steps(data=diabetes.steps_,var=steps_et,week_start=37,week_end=40);
%unadjusted_steps(data=diabetes.steps_,var=steps_et,week_start=41,week_end=44);
%unadjusted_steps(data=diabetes.steps_,var=steps_et,week_start=45,week_end=48);
%unadjusted_steps(data=diabetes.steps_,var=steps_et,week_start=49,week_end=52);

%unadjusted_mean(data=diabetes.participant_level_data,var=baseline_sleep,round=1);
%unadjusted_steps(data=diabetes.sleep1,var=total_sleep_duration_m,week_start=1,week_end=26);
%unadjusted_steps(data=diabetes.sleep1,var=total_sleep_duration_m,week_start=27,week_end=52);
%unadjusted_steps(data=diabetes.sleep1,var=total_sleep_duration_m,week_start=1,week_end=52);

%unadjusted_mean(data=diabetes.participant_level_data,var=baseline_sleep,round=1);
%unadjusted_steps(data=diabetes.sleep1,var=total_sleep_duration_m,week_start=1,week_end=4);
%unadjusted_steps(data=diabetes.sleep1,var=total_sleep_duration_m,week_start=5,week_end=8);
%unadjusted_steps(data=diabetes.sleep1,var=total_sleep_duration_m,week_start=9,week_end=12);
%unadjusted_steps(data=diabetes.sleep1,var=total_sleep_duration_m,week_start=13,week_end=16);
%unadjusted_steps(data=diabetes.sleep1,var=total_sleep_duration_m,week_start=17,week_end=20);
%unadjusted_steps(data=diabetes.sleep1,var=total_sleep_duration_m,week_start=21,week_end=24);
%unadjusted_steps(data=diabetes.sleep1,var=total_sleep_duration_m,week_start=25,week_end=28);
%unadjusted_steps(data=diabetes.sleep1,var=total_sleep_duration_m,week_start=29,week_end=32);
%unadjusted_steps(data=diabetes.sleep1,var=total_sleep_duration_m,week_start=33,week_end=36);
%unadjusted_steps(data=diabetes.sleep1,var=total_sleep_duration_m,week_start=37,week_end=40);
%unadjusted_steps(data=diabetes.sleep1,var=total_sleep_duration_m,week_start=41,week_end=44);
%unadjusted_steps(data=diabetes.sleep1,var=total_sleep_duration_m,week_start=45,week_end=48);
%unadjusted_steps(data=diabetes.sleep1,var=total_sleep_duration_m,week_start=49,week_end=52);
/*Monthly step trends: Imputed Data*/
%macro monthly_step_trends(data=,var=,week_start=,week_end=);
proc means data=&data nway noprint;
where &week_start<=week<=&week_end;
class arm_id participant_id;
var &var;
output out=mean mean=mean ;
run;
proc means data=mean nway noprint ;
class arm_id;
var mean;
output out=mean1 mean=mean std=std;
run;
data mean1_;
set mean1;
mean1=round(mean,1);
run;
proc transpose data=mean1_ out=mean1_wide;
var mean1;
id arm_id;
run;
proc print data=mean1_wide noobs;
var A B C D;
run;
%mend;
%unadjusted_mean(data=diabetes.participant_level_data,var=baseline_steps,round=1);
%monthly_step_trends(data=diabetes.steps_imputed_,var=steps1000_et,week_start=1,week_end=4);
%monthly_step_trends(data=diabetes.steps_imputed_,var=steps1000_et,week_start=5,week_end=8);
%monthly_step_trends(data=diabetes.steps_imputed_,var=steps1000_et,week_start=9,week_end=12);
%monthly_step_trends(data=diabetes.steps_imputed_,var=steps1000_et,week_start=13,week_end=16);
%monthly_step_trends(data=diabetes.steps_imputed_,var=steps1000_et,week_start=17,week_end=20);
%monthly_step_trends(data=diabetes.steps_imputed_,var=steps1000_et,week_start=21,week_end=24);
%monthly_step_trends(data=diabetes.steps_imputed_,var=steps1000_et,week_start=25,week_end=28);
%monthly_step_trends(data=diabetes.steps_imputed_,var=steps1000_et,week_start=29,week_end=32);
%monthly_step_trends(data=diabetes.steps_imputed_,var=steps1000_et,week_start=33,week_end=36);
%monthly_step_trends(data=diabetes.steps_imputed_,var=steps1000_et,week_start=37,week_end=40);
%monthly_step_trends(data=diabetes.steps_imputed_,var=steps1000_et,week_start=41,week_end=44);
%monthly_step_trends(data=diabetes.steps_imputed_,var=steps1000_et,week_start=45,week_end=48);
%monthly_step_trends(data=diabetes.steps_imputed_,var=steps1000_et,week_start=49,week_end=52);

%unadjusted_mean(data=diabetes.participant_level_data,var=baseline_steps,round=1);
%monthly_step_trends(data=diabetes.steps_imputed_,var=steps1000_et_change,week_start=1,week_end=4);
%monthly_step_trends(data=diabetes.steps_imputed_,var=steps1000_et_change,week_start=5,week_end=8);
%monthly_step_trends(data=diabetes.steps_imputed_,var=steps1000_et_change,week_start=9,week_end=12);
%monthly_step_trends(data=diabetes.steps_imputed_,var=steps1000_et_change,week_start=13,week_end=16);
%monthly_step_trends(data=diabetes.steps_imputed_,var=steps1000_et_change,week_start=17,week_end=20);
%monthly_step_trends(data=diabetes.steps_imputed_,var=steps1000_et_change,week_start=21,week_end=24);
%monthly_step_trends(data=diabetes.steps_imputed_,var=steps1000_et_change,week_start=25,week_end=28);
%monthly_step_trends(data=diabetes.steps_imputed_,var=steps1000_et_change,week_start=29,week_end=32);
%monthly_step_trends(data=diabetes.steps_imputed_,var=steps1000_et_change,week_start=33,week_end=36);
%monthly_step_trends(data=diabetes.steps_imputed_,var=steps1000_et_change,week_start=37,week_end=40);
%monthly_step_trends(data=diabetes.steps_imputed_,var=steps1000_et_change,week_start=41,week_end=44);
%monthly_step_trends(data=diabetes.steps_imputed_,var=steps1000_et_change,week_start=45,week_end=48);
%monthly_step_trends(data=diabetes.steps_imputed_,var=steps1000_et_change,week_start=49,week_end=52);

%unadjusted_mean(data=diabetes.participant_level_data,var=baseline_steps_less1000,round=1);
%monthly_step_trends(data=diabetes.steps_imputed_,var=steps_et,week_start=1,week_end=4);
%monthly_step_trends(data=diabetes.steps_imputed_,var=steps_et,week_start=5,week_end=8);
%monthly_step_trends(data=diabetes.steps_imputed_,var=steps_et,week_start=9,week_end=12);
%monthly_step_trends(data=diabetes.steps_imputed_,var=steps_et,week_start=13,week_end=16);
%monthly_step_trends(data=diabetes.steps_imputed_,var=steps_et,week_start=17,week_end=20);
%monthly_step_trends(data=diabetes.steps_imputed_,var=steps_et,week_start=21,week_end=24);
%monthly_step_trends(data=diabetes.steps_imputed_,var=steps_et,week_start=25,week_end=28);
%monthly_step_trends(data=diabetes.steps_imputed_,var=steps_et,week_start=29,week_end=32);
%monthly_step_trends(data=diabetes.steps_imputed_,var=steps_et,week_start=33,week_end=36);
%monthly_step_trends(data=diabetes.steps_imputed_,var=steps_et,week_start=37,week_end=40);
%monthly_step_trends(data=diabetes.steps_imputed_,var=steps_et,week_start=41,week_end=44);
%monthly_step_trends(data=diabetes.steps_imputed_,var=steps_et,week_start=45,week_end=48);
%monthly_step_trends(data=diabetes.steps_imputed_,var=steps_et,week_start=49,week_end=52);

%unadjusted_mean(data=diabetes.participant_level_data,var=baseline_steps_less1000,round=1);
%monthly_step_trends(data=diabetes.steps_imputed_,var=steps_et_change,week_start=1,week_end=4);
%monthly_step_trends(data=diabetes.steps_imputed_,var=steps_et_change,week_start=5,week_end=8);
%monthly_step_trends(data=diabetes.steps_imputed_,var=steps_et_change,week_start=9,week_end=12);
%monthly_step_trends(data=diabetes.steps_imputed_,var=steps_et_change,week_start=13,week_end=16);
%monthly_step_trends(data=diabetes.steps_imputed_,var=steps_et_change,week_start=17,week_end=20);
%monthly_step_trends(data=diabetes.steps_imputed_,var=steps_et_change,week_start=21,week_end=24);
%monthly_step_trends(data=diabetes.steps_imputed_,var=steps_et_change,week_start=25,week_end=28);
%monthly_step_trends(data=diabetes.steps_imputed_,var=steps_et_change,week_start=29,week_end=32);
%monthly_step_trends(data=diabetes.steps_imputed_,var=steps_et_change,week_start=33,week_end=36);
%monthly_step_trends(data=diabetes.steps_imputed_,var=steps_et_change,week_start=37,week_end=40);
%monthly_step_trends(data=diabetes.steps_imputed_,var=steps_et_change,week_start=41,week_end=44);
%monthly_step_trends(data=diabetes.steps_imputed_,var=steps_et_change,week_start=45,week_end=48);
%monthly_step_trends(data=diabetes.steps_imputed_,var=steps_et_change,week_start=49,week_end=52);
/*Monthly step trends: Collected Data*/

%unadjusted_mean(data=diabetes.participant_level_data,var=baseline_steps,round=1);
%monthly_step_trends(data=diabetes.steps_,var=steps1000_et,week_start=1,week_end=4);
%monthly_step_trends(data=diabetes.steps_,var=steps1000_et,week_start=5,week_end=8);
%monthly_step_trends(data=diabetes.steps_,var=steps1000_et,week_start=9,week_end=12);
%monthly_step_trends(data=diabetes.steps_,var=steps1000_et,week_start=13,week_end=16);
%monthly_step_trends(data=diabetes.steps_,var=steps1000_et,week_start=17,week_end=20);
%monthly_step_trends(data=diabetes.steps_,var=steps1000_et,week_start=21,week_end=24);
%monthly_step_trends(data=diabetes.steps_,var=steps1000_et,week_start=25,week_end=28);
%monthly_step_trends(data=diabetes.steps_,var=steps1000_et,week_start=29,week_end=32);
%monthly_step_trends(data=diabetes.steps_,var=steps1000_et,week_start=33,week_end=36);
%monthly_step_trends(data=diabetes.steps_,var=steps1000_et,week_start=37,week_end=40);
%monthly_step_trends(data=diabetes.steps_,var=steps1000_et,week_start=41,week_end=44);
%monthly_step_trends(data=diabetes.steps_,var=steps1000_et,week_start=45,week_end=48);
%monthly_step_trends(data=diabetes.steps_,var=steps1000_et,week_start=49,week_end=52);

%unadjusted_mean(data=diabetes.participant_level_data,var=baseline_steps,round=1);
%monthly_step_trends(data=diabetes.steps_,var=steps1000_et_change,week_start=1,week_end=4);
%monthly_step_trends(data=diabetes.steps_,var=steps1000_et_change,week_start=5,week_end=8);
%monthly_step_trends(data=diabetes.steps_,var=steps1000_et_change,week_start=9,week_end=12);
%monthly_step_trends(data=diabetes.steps_,var=steps1000_et_change,week_start=13,week_end=16);
%monthly_step_trends(data=diabetes.steps_,var=steps1000_et_change,week_start=17,week_end=20);
%monthly_step_trends(data=diabetes.steps_,var=steps1000_et_change,week_start=21,week_end=24);
%monthly_step_trends(data=diabetes.steps_,var=steps1000_et_change,week_start=25,week_end=28);
%monthly_step_trends(data=diabetes.steps_,var=steps1000_et_change,week_start=29,week_end=32);
%monthly_step_trends(data=diabetes.steps_,var=steps1000_et_change,week_start=33,week_end=36);
%monthly_step_trends(data=diabetes.steps_,var=steps1000_et_change,week_start=37,week_end=40);
%monthly_step_trends(data=diabetes.steps_,var=steps1000_et_change,week_start=41,week_end=44);
%monthly_step_trends(data=diabetes.steps_,var=steps1000_et_change,week_start=45,week_end=48);
%monthly_step_trends(data=diabetes.steps_,var=steps1000_et_change,week_start=49,week_end=52);

%unadjusted_mean(data=diabetes.participant_level_data,var=baseline_steps_less1000,round=1);
%monthly_step_trends(data=diabetes.steps_,var=steps_et,week_start=1,week_end=4);
%monthly_step_trends(data=diabetes.steps_,var=steps_et,week_start=5,week_end=8);
%monthly_step_trends(data=diabetes.steps_,var=steps_et,week_start=9,week_end=12);
%monthly_step_trends(data=diabetes.steps_,var=steps_et,week_start=13,week_end=16);
%monthly_step_trends(data=diabetes.steps_,var=steps_et,week_start=17,week_end=20);
%monthly_step_trends(data=diabetes.steps_,var=steps_et,week_start=21,week_end=24);
%monthly_step_trends(data=diabetes.steps_,var=steps_et,week_start=25,week_end=28);
%monthly_step_trends(data=diabetes.steps_,var=steps_et,week_start=29,week_end=32);
%monthly_step_trends(data=diabetes.steps_,var=steps_et,week_start=33,week_end=36);
%monthly_step_trends(data=diabetes.steps_,var=steps_et,week_start=37,week_end=40);
%monthly_step_trends(data=diabetes.steps_,var=steps_et,week_start=41,week_end=44);
%monthly_step_trends(data=diabetes.steps_,var=steps_et,week_start=45,week_end=48);
%monthly_step_trends(data=diabetes.steps_,var=steps_et,week_start=49,week_end=52);

%unadjusted_mean(data=diabetes.participant_level_data,var=baseline_steps_less1000,round=1);
%monthly_step_trends(data=diabetes.steps_,var=steps_et_change,week_start=1,week_end=4);
%monthly_step_trends(data=diabetes.steps_,var=steps_et_change,week_start=5,week_end=8);
%monthly_step_trends(data=diabetes.steps_,var=steps_et_change,week_start=9,week_end=12);
%monthly_step_trends(data=diabetes.steps_,var=steps_et_change,week_start=13,week_end=16);
%monthly_step_trends(data=diabetes.steps_,var=steps_et_change,week_start=17,week_end=20);
%monthly_step_trends(data=diabetes.steps_,var=steps_et_change,week_start=21,week_end=24);
%monthly_step_trends(data=diabetes.steps_,var=steps_et_change,week_start=25,week_end=28);
%monthly_step_trends(data=diabetes.steps_,var=steps_et_change,week_start=29,week_end=32);
%monthly_step_trends(data=diabetes.steps_,var=steps_et_change,week_start=33,week_end=36);
%monthly_step_trends(data=diabetes.steps_,var=steps_et_change,week_start=37,week_end=40);
%monthly_step_trends(data=diabetes.steps_,var=steps_et_change,week_start=41,week_end=44);
%monthly_step_trends(data=diabetes.steps_,var=steps_et_change,week_start=45,week_end=48);
%monthly_step_trends(data=diabetes.steps_,var=steps_et_change,week_start=49,week_end=52);
/*Unadj A1c (imp) trends*/
proc contents data=diabetes.hba1c_imputed_;
run;
proc sql;
create table base_12_a1c as
select distinct participant_id,arm_id,Enrollment_A1C,mean(twelve_month_a1c_coal) as twelve_month_A1C
from diabetes.hba1c_imputed_
group by participant_id
order by arm_id
;
quit;
proc print data=base_12_a1c noobs;
run;
/*Adjusted Models: Imputed Data*/
proc contents data=diabetes.hba1c_imputed_;
run;
proc contents data=diabetes.weights_imputed_;
run;
proc contents data=diabetes.LDL_imputed_;
run;

%macro unadjust_mixed_mi(data=,var=,month=,baseline=,round=);
proc sort data=&data;
by _imputation_;
run;
proc glimmix data=&data;
class arm_id(ref="D") &month /*Diabetes__insulin(ref="0") Diabetes__oral(ref="0")*/;
model &var=arm_id &month &baseline /*Diabetes__insulin Diabetes__oral*//solution;
lsmeans arm_id/pdiff cl;
by _imputation_;
ods output diffs=diff lsmeans=lsmeans classlevels=classlevels;
run;
data diff2;
      set diff;
      comparison=arm_id||' vs '||left(_arm_id);
	  if _arm_id="D";
      run; 
   proc sort data=diff2;
      by comparison _imputation_;
      run;
   ods trace on;
   proc mianalyze data=diff2;
      by comparison;
      modeleffects estimate;
      stderr stderr;
	  ods output parameterestimates=parest;
   run;
   ods trace off;
data parest1;
set parest;
est_cl=cat(round(Estimate,&round)," (",round(LCLMean,&round),", ",round(UCLMean,&round),")");
run;
proc transpose data=parest1 out=estimates;
var est_cl;
id comparison;
run;
title "&var.";
proc print data=estimates noobs;
var _NAME_ A_vs_D B_vs_D C_vs_D ;
run;
proc transpose data=parest1 out=p_value;
var probt;
id comparison;
run;
proc print data=p_value noobs;
var _NAME_ A_vs_D B_vs_D C_vs_D ;
run;
title;
%mend;
%unadjust_mixed_mi(data=diabetes.hba1c_imputed_,var=six_month_a1c_coal,month=sixth_month,baseline=Enrollment_A1C,round=.01);
%unadjust_mixed_mi(data=diabetes.hba1c_imputed_,var=twelve_month_a1c_coal,month=twelfth_month,baseline=Enrollment_A1C,round=.01);
%unadjust_mixed_mi(data=diabetes.weights_imputed_,var=six_month_wt_coal,month=sixth_month,baseline=baselinewt,round=.1);
%unadjust_mixed_mi(data=diabetes.weights_imputed_,var=twelve_month_wt_coal,month=twelfth_month,baseline=baselinewt,round=.1);
%unadjust_mixed_mi(data=diabetes.LDL_imputed_,var=six_month_LDL_coal,month=sixth_month,baseline=Enrollment_LDL1,round=.1);
%unadjust_mixed_mi(data=diabetes.LDL_imputed_,var=twelve_month_LDL_coal,month=twelfth_month,baseline=Enrollment_LDL1,round=.1);

proc contents data=diabetes.steps_imputed_;
run;
proc contents data=diabetes.sleep_imputed_;
run;
%macro unadjust_steps_mixed_mi(data=,week1=,week2=,var=,baseline=,round=);
proc sort data=&data;
by _imputation_;
run;
proc glimmix data=&data;
where &week1<=week<=&week2 ;
class arm_id(ref="D") month ;
model &var=arm_id month &baseline/solution;
lsmeans arm_id/pdiff cl;
random intercept/subject=participant_id type=ar(1);
by _imputation_;
ods output diffs=diff lsmeans=lsmeans classlevels=classlevels;
run;
data diff2;
      set diff;
      comparison=arm_id||' vs '||left(_arm_id);
	  if _arm_id="D";
      run;
    proc sort data=diff2;
      by comparison _imputation_;
      run;
   ods trace on;
   proc mianalyze data=diff2;
      by comparison;
      modeleffects estimate;
      stderr stderr;
	  ods output parameterestimates=parest;
   run;
   ods trace off;
data parest1;
set parest;
est_cl=cat(round(Estimate,&round)," (",round(LCLMean,&round),", ",round(UCLMean,&round),")");
run;
title "&var. : Weeks &week1. to &week2";
proc transpose data=parest1 out=estimates;
var est_cl;
id comparison;
run;
proc print data=estimates noobs;
var _NAME_ A_vs_D B_vs_D C_vs_D ;
run;
proc transpose data=parest1 out=p_value;
var probt;
id comparison;
run;
proc print data=p_value noobs;
var _NAME_ A_vs_D B_vs_D C_vs_D ;
run;
title;
%mend;
proc print data=parest;
run;
%unadjust_steps_mixed_mi(data=diabetes.steps_imputed_,week1=1,week2=26,var=steps1000_et,baseline=baseline_steps,round=.1);
%unadjust_steps_mixed_mi(data=diabetes.steps_imputed_,week1=1,week2=52,var=steps1000_et,baseline=baseline_steps,round=.1);

%unadjust_steps_mixed_mi(data=diabetes.steps_imputed_,week1=1,week2=26,var=steps_et,baseline=baseline_steps_less1000,round=.1);
%unadjust_steps_mixed_mi(data=diabetes.steps_imputed_,week1=1,week2=52,var=steps_et,baseline=baseline_steps_less1000,round=.1);

%unadjust_steps_mixed_mi(data=diabetes.sleep_imputed_,week1=1,week2=26,var=total_sleep_duration_m,baseline=baseline_sleep,round=.1);
%unadjust_steps_mixed_mi(data=diabetes.sleep_imputed_,week1=1,week2=52,var=total_sleep_duration_m,baseline=baseline_sleep,round=.1);
/*Adjusted Models: Collected Data*/


%macro glimmix_model(data=,var=,baseline=,month=,round=);
proc glimmix data=&data;
class arm_id(ref="D") &month ;
model &var = arm_id &baseline &month /solution cl;
lsmeans arm_id/pdiff cl;
ods output Diffs=Diffs ParameterEstimates=&var.;
quit;
data &var.1;
set &var. (rename=(Probt=p_value));
Estimate_CL=cat(round(estimate,&round)," (",round(Lower,&round),"-",round(Upper,&round),")");
run;
data Diffs1;
set Diffs;
if _arm_id^="D" then delete;
arms=cat(arm_id,"_",_arm_id);
diff_cl=cat(round(estimate,&round)," (",round(Lower,&round),"-",round(Upper,&round),")");
run;
proc transpose data=Diffs1 out=Diffs1_dc;
var diff_cl;
id arms;
run;
proc print data=Diffs1_dc noobs;
var _NAME_ A_D B_D C_D ;
run;
proc transpose data=Diffs1 out=Diffs1_p;
var Probt;
id arms;
run;
proc print data=Diffs1_p noobs;
var _NAME_ A_D B_D C_D ;
run;
%mend;
%glimmix_model(data=diabetes.participant_level_data,var=six_month_a1c_coal,baseline=Enrollment_A1C,month=sixth_month_A1C,round=0.01);
%glimmix_model(data=diabetes.participant_level_data,var=six_month_wt_coal,baseline=baselinewt,month=sixth_month_wt,round=0.1);
%glimmix_model(data=diabetes.participant_level_data,var=six_month_LDL_coal,baseline=Enrollment_LDL1,month=sixth_month_LDL,round=0.1);

%glimmix_model(data=diabetes.participant_level_data,var=twelve_month_a1c_coal,baseline=Enrollment_A1C,month=twelfth_month_A1C,round=0.01);
%glimmix_model(data=diabetes.participant_level_data,var=twelve_month_wt_coal,baseline=baselinewt,month=twelfth_month_wt,round=0.1);
%glimmix_model(data=diabetes.participant_level_data,var=twelve_month_LDL_coal,baseline=Enrollment_LDL1,month=twelfth_month_LDL,round=0.1);

proc contents data=diabetes.steps_ varnum;
run;
proc contents data=data=diabetes.sleep2;
run;

%macro glimmix_model_steps(data=,var=,baseline=,week1=,week2=,round=);
proc glimmix data=&data;
where &week1<=week<=&week2;
class arm_id(ref="D") month ;
model &var = arm_id &baseline month /solution cl;
random intercept/subject=participant_id type=ar(1);
lsmeans arm_id/pdiff cl;
ods output Diffs=Diffs ParameterEstimates=PE&var.&week1.&week2.;
quit;
data PE&var.&week1.&week2.1;
set PE&var.&week1.&week2. (rename=(Probt=p_value));
Estimate_CL=cat(round(estimate,&round)," (",round(Lower,&round),"-",round(Upper,&round),")");
run;
data Diffs1;
set Diffs;
if _arm_id^="D" then delete;
arms=cat(arm_id,"_",_arm_id);
diff_cl=cat(round(estimate,&round)," (",round(Lower,&round),"-",round(Upper,&round),")");
run;
proc transpose data=Diffs1 out=Diffs1_dc;
var diff_cl;
id arms;
run;
proc print data=Diffs1_dc noobs;
var _NAME_ A_D B_D C_D ;
run;
proc transpose data=Diffs1 out=Diffs1_p;
var Probt;
id arms;
run;
proc print data=Diffs1_p noobs;
var _NAME_ A_D B_D C_D ;
run;
%mend;
%glimmix_model_steps(data=diabetes.steps_,var=steps1000_et,baseline=baseline_steps,week1=1,week2=26,round=.1);
%glimmix_model_steps(data=diabetes.steps_,var=steps1000_et,baseline=baseline_steps,week1=1,week2=52,round=.1);
/*baseline_steps_less1000 is the baseline steps allowing for step values less than 1000*/
%glimmix_model_steps(data=diabetes.steps_,var=steps_et,baseline=baseline_steps_less1000,week1=1,week2=26,round=.1);
%glimmix_model_steps(data=diabetes.steps_,var=steps_et,baseline=baseline_steps_less1000,week1=1,week2=52,round=.1);

%glimmix_model_steps(data=diabetes.sleep2,var=total_sleep_duration_m,baseline=baseline_sleep,week1=1,week2=26,round=.1);
%glimmix_model_steps(data=diabetes.sleep2,var=total_sleep_duration_m,baseline=baseline_sleep,week1=1,week2=52,round=.1);

/*Additional Models*/
/*Imputed:
"Hemoglobin A1c
(C as referent)"
*/
proc sort data=diabetes.hba1c_imputed_;
by _imputation_;
run;
proc glimmix data=diabetes.hba1c_imputed_;
class arm_id(ref="C") twelfth_month;
model twelve_month_a1c_coal=arm_id twelfth_month Enrollment_A1C/solution;
lsmeans arm_id/pdiff cl;
by _imputation_;
ods output diffs=diff lsmeans=lsmeans classlevels=classlevels;
run;
data diff2;
      set diff;
      comparison=arm_id||' vs '||left(_arm_id);
	  if _arm_id="C";
      run; 
   proc sort data=diff2;
      by comparison _imputation_;
      run;
   ods trace on;
   proc mianalyze data=diff2;
      by comparison;
      modeleffects estimate;
      stderr stderr;
	  ods output parameterestimates=parest;
   run;
   ods trace off;
data parest1;
set parest;
est_cl=cat(round(Estimate,.01)," (",round(LCLMean,.01),", ",round(UCLMean,.01),")");
run;
proc transpose data=parest1 out=estimates;
var est_cl;
id comparison;
run;
title "twelve_month_a1c_coal";
proc print data=estimates noobs;
var _NAME_ A_vs_C B_vs_C D_vs_C ;
run;
proc transpose data=parest1 out=p_value;
var probt;
id comparison;
run;
proc print data=p_value noobs;
var _NAME_ A_vs_C B_vs_C D_vs_C ;
run;
title;
/*Imputed data:
"Hemoglobin A1c
(with Diabetes medications)"
*/
%macro unadjust_mixed_mi_meds(data=,var=,month=,baseline=,round=);
proc sort data=&data;
by _imputation_;
run;
proc glimmix data=&data;
class arm_id(ref="D") &month Diabetes__insulin(ref="0") Diabetes__oral(ref="0");
model &var=arm_id &month &baseline Diabetes__insulin Diabetes__oral/solution;
lsmeans arm_id/pdiff cl;
by _imputation_;
ods output diffs=diff lsmeans=lsmeans classlevels=classlevels;
run;
data diff2;
      set diff;
      comparison=arm_id||' vs '||left(_arm_id);
	  if _arm_id="D";
      run; 
   proc sort data=diff2;
      by comparison _imputation_;
      run;
   ods trace on;
   proc mianalyze data=diff2;
      by comparison;
      modeleffects estimate;
      stderr stderr;
	  ods output parameterestimates=parest;
   run;
   ods trace off;
data parest1;
set parest;
est_cl=cat(round(Estimate,&round)," (",round(LCLMean,&round),", ",round(UCLMean,&round),")");
run;
proc transpose data=parest1 out=estimates;
var est_cl;
id comparison;
run;
title "&var.";
proc print data=estimates noobs;
var _NAME_ A_vs_D B_vs_D C_vs_D ;
run;
proc transpose data=parest1 out=p_value;
var probt;
id comparison;
run;
proc print data=p_value noobs;
var _NAME_ A_vs_D B_vs_D C_vs_D ;
run;
title;
%mend;
%unadjust_mixed_mi_meds(data=diabetes.hba1c_imputed_,var=six_month_a1c_coal,month=sixth_month,baseline=Enrollment_A1C,round=.01);
%unadjust_mixed_mi_meds(data=diabetes.hba1c_imputed_,var=twelve_month_a1c_coal,month=twelfth_month,baseline=Enrollment_A1C,round=.01);
/*Collected data:
"Hemoglobin A1c
(with Diabetes medications)"
*/
%macro glimmix_model_meds(data=,var=,baseline=,month=,round=);
proc glimmix data=&data;
class arm_id(ref="D") &month Diabetes__insulin(ref="0") Diabetes__oral(ref="0");
model &var = arm_id &baseline &month Diabetes__insulin Diabetes__oral/solution cl;
lsmeans arm_id/pdiff cl;
ods output Diffs=Diffs ParameterEstimates=&var.;
quit;
data &var.1;
set &var. (rename=(Probt=p_value));
Estimate_CL=cat(round(estimate,&round)," (",round(Lower,&round),"-",round(Upper,&round),")");
run;
data Diffs1;
set Diffs;
if _arm_id^="D" then delete;
arms=cat(arm_id,"_",_arm_id);
diff_cl=cat(round(estimate,&round)," (",round(Lower,&round),"-",round(Upper,&round),")");
run;
proc transpose data=Diffs1 out=Diffs1_dc;
var diff_cl;
id arms;
run;
proc print data=Diffs1_dc noobs;
var _NAME_ A_D B_D C_D ;
run;
proc transpose data=Diffs1 out=Diffs1_p;
var Probt;
id arms;
run;
proc print data=Diffs1_p noobs;
var _NAME_ A_D B_D C_D ;
run;
%mend;
%glimmix_model_meds(data=diabetes.participant_level_data,var=six_month_a1c_coal,baseline=Enrollment_A1C,month=sixth_month_A1c,round=0.01);
%glimmix_model_meds(data=diabetes.participant_level_data,var=twelve_month_a1c_coal,baseline=Enrollment_A1C,month=twelfth_month_A1c,round=0.01);
/*Survey Measures*/
/*Big Five*/
%cont_summary(data=diabetes.participant_level_data,var=bf_extroversion,round=0.1);
%cont_summary(data=diabetes.participant_level_data,var=bf_agreeableness,round=0.1);
%cont_summary(data=diabetes.participant_level_data,var=bf_conscientious,round=0.1);
%cont_summary(data=diabetes.participant_level_data,var=bf_neuroticism,round=0.1);
%cont_summary(data=diabetes.participant_level_data,var=bf_openness,round=0.1);
/*MOSSS*/
%cont_summary(data=diabetes.participant_level_data,var=emotional_score,round=0.1);
%cont_summary(data=diabetes.participant_level_data,var=tangible_score,round=0.1);
%cont_summary(data=diabetes.participant_level_data,var=affectionate_score,round=0.1);
%cont_summary(data=diabetes.participant_level_data,var=positive_soc_int_score,round=0.1);
%cont_summary(data=diabetes.participant_level_data,var=additional_score,round=0.1);
%cont_summary(data=diabetes.participant_level_data,var=mosss_overall,round=0.1);
%cont_summary(data=diabetes.participant_level_data,var=mosss_0_100,round=0.1);

/*DOSPERT*/
%cont_summary(data=diabetes.participant_level_data,var=dospert_ethical,round=0.1);
%cont_summary(data=diabetes.participant_level_data,var=dospert_financial,round=0.1);
%cont_summary(data=diabetes.participant_level_data,var=dospert_health_safety,round=0.1);
%cont_summary(data=diabetes.participant_level_data,var=dospert_recreational,round=0.1);
%cont_summary(data=diabetes.participant_level_data,var=dospert_social,round=0.1);




/*End of Study Surveys*/
%macro endofstudy(var=);
proc freq data=diabetes.participant_level_data noprint;
tables (&var)*arm_id/norow nopercent out=&var outpct;
run;
data &var.1;
set &var;
n_pct=cat(count," (",round(PCT_COL,.1),"%)");
where &var^=.;
run;
proc sort data=&var.1 ;
by &var;
run;
proc transpose data=&var.1 out=&var.1_w;
by &var;
var n_pct;
id arm_id;
run;
proc sort data=&var.1_w;
by descending &var;
run;
proc print data=&var.1_w noobs;
var &var;
run;
proc print data=&var.1_w noobs;
var A B C D;
run;
proc freq data=diabetes.endofstudy_1 noprint;
tables &var/out=&var._;
run;
data &var._1;
set &var._;
n_pct=cat(count," (",round(PERCENT,.1),"%)");
where &var^=.;
run;
proc sort data=&var._1;
by descending &var;
run;
proc print data=&var._1 noobs;
var &var;
run;
proc print data=&var._1 noobs;
var n_pct;
run;
%mend;
%endofstudy(var=incpa);
%endofstudy(var=losewt);
%endofstudy(var=improveDM);
%endofstudy(var=satisfysteps);
%endofstudy(var=satisfyscale);
%endofstudy(var=likedstdy);
